package com.cmfi.controller.userRoleAuthController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cmfi.domain.userRoleAuthEntity.Role;
import com.cmfi.service.inter.userRoleAuthInter.RoleService;
import com.cmfi.utils.Code;
import com.cmfi.utils.Result;

@RestController
@RequestMapping("/api")
public class RoleController {

    private final Logger log = LoggerFactory.getLogger(RoleController.class);
    @Autowired
    private RoleService roleService;

    @PostMapping("/role")
    public Result createRole(@RequestBody Role role) {
        try {
        	boolean flag = roleService.saveRole(role);
    		return new Result(Code.SAVE_OK, flag);
    	} catch (Exception e) {
    		log.error("[UserController][createUser]  e,", e);
    		return new Result(Code.SAVE_ERR, e.getMessage());
    	}
    }

	@DeleteMapping("/role/{id}")
	public Result delRole(@PathVariable(value = "id", required = false) final Long id) {
		try {
			roleService.delRole(id);
			return new Result(Code.SAVE_OK, id);
		} catch (Exception e) {
			log.error("[UserController][delRole]  e,", e);
			return new Result(Code.SAVE_ERR, e.getMessage());
		}
    }
	
	/**
     * {@code GET  /forklift-qualities} : get all the forkliftQualities.
     *
     * @param pageable the pagination information.
     * @param request a {@link ServerHttpRequest} request.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of forkliftQualities in body.
     */
	@GetMapping("/role/{id}")
	public Result getRoleById(@PathVariable(value = "id", required = false) final Long id) {
		try {
			Role role = roleService.getRoleById(id);
			return new Result(Code.GET_OK, role);
		} catch (Exception e) {
			log.error("[ForkliftQualityController][getAllForkliftQualities] e,", e);
			return new Result(Code.GET_ERR, e.getMessage());
		}
	}
	
	/**
	 * {@code GET  /forklift-qualities} : get all the forkliftQualities.
	 *
	 * @param pageable the pagination information.
	 * @param request  a {@link ServerHttpRequest} request.
	 * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list
	 *         of forkliftQualities in body.
	 */
	@GetMapping("/authorities")
	public Result getAllRoles() {
		try {
			List<String> roleList = new ArrayList<>();
			roleList = roleService.getAllRoles();
			long total = roleList.size();
			return new Result(Code.GET_OK, roleList, total);
		} catch (Exception e) {
			log.error("[ForkliftQualityController][getAllForkliftQualities] e,", e);
			return new Result(Code.GET_ERR, e.getMessage());
		}
	}
	
//	@GetMapping("/authorities")
//	public List<String> getAllRoles() {
//		try {
//			List<String> roleList = new ArrayList<>();
//			roleList = roleService.getAllRoles();
//			long total = roleList.size();
//			return roleList,total;
//		} catch (Exception e) {
//			log.error("[ForkliftQualityController][getAllForkliftQualities] e,", e);
//			return new ArrayList<>();
//		}
//	}

	@PutMapping("/role/user/authorize")
	 @PreAuthorize("hasAuthority('basicDataManage')")
	public Result authorizePermissionForRole(@RequestParam Map<String, Object> params) {
		try {
			Long menuAuthId = null;
			Long roleId = null;
			if (params.containsKey("menuAuthId")) {
				menuAuthId = Long.parseLong(params.get("menuAuthId").toString());
			}
			if (params.containsKey("roleId")) {
				roleId = Long.parseLong(params.get("roleId").toString());
			}
			roleService.authorizeMenuAuthForRole(menuAuthId, roleId);
			return new Result(Code.UPDATE_OK, menuAuthId.toString());
		} catch (Exception e) {
			log.error("[UserController][authorizePermissionForRole]  e,", e);
			return new Result(Code.UPDATE_ERR, e.getMessage());
		}
	}

}
